package com.cxy.instarsystem.service;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.cxy.instarsystem.dao.NewsRepository;
import com.cxy.instarsystem.po.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsRepository newsRepository;

    @Override
    public News getNews(Long id) {
        News news = newsRepository.getNewsById(id);
        String content = "<p>" + news.getContent().replace("\n","</p><p>") + "</p>";
        news.setContent(content);
//        System.out.println(news.getContent());
        return news;
    }

    @Override
    public List<News> listTypeTop(Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(0, size, sort);
        return newsRepository.findTop(pageable);
    }


}
